﻿$(function() {
  handleScreen();
  handleSearch();
  //handleMail();
  //handleNotify();
  handleLoginOut();
  handleSkin();
});


//全屏
var fullScreen = function () {
  var elem = document.documentElement;
    var requestMethod = elem.requestFullScreen || elem.webkitRequestFullScreen || elem.mozRequestFullScreen || elem.msRequestFullScreen;

  if (requestMethod) {
      requestMethod.call(elem);
  } else if (typeof window.ActiveXObject !== "undefined") {
    var wscript = new ActiveXObject("WScript.Shell");
    if (wscript !== null) {
      wscript.SendKeys("{F11}");
    }
  }
}

//退出全屏
var exitFullScreen = function () {
  var elem = document;
  if (elem.webkitCancelFullScreen) {
    elem.webkitCancelFullScreen();
  } else if (elem.mozCancelFullScreen) {
    elem.mozCancelFullScreen();
  } else if (elem.cancelFullScreen) {
    elem.cancelFullScreen();
  } else if (elem.exitFullscreen) {
    elem.exitFullscreen();
  } else {
    //  notice.notice_show("浏览器不支持全屏API或已被禁用", null, null, null, true, true);
  }
}

//全屏切换
var handleScreen = function() {
  $("#fullscreenToggle").click(function () {
    if ($("#fullscreenToggle").data("state") === "fullscreen") {
      $("#fullscreenToggle").data("state", "");
      $("#fullscreenToggle").find("i").removeClass("icon-huanyuan");
      $("#fullscreenToggle").find("i").addClass("icon-quanping");
      exitFullScreen();
    } else {
      $("#fullscreenToggle").data("state", "fullscreen");
      $("#fullscreenToggle").find("i").removeClass("icon-quanping");
      $("#fullscreenToggle").find("i").addClass("icon-huanyuan");
      fullScreen();
    }
  });
}

//处理搜索框
var handleSearch = function()
{
  var oldValue = "";
  var needReloadMenu = false;

  var onReloadMenuTree = function () {
    if (needReloadMenu) {
      needReloadMenu = false;
      oldValue = $("#searchStr").val();
      layer.load(2);
      $.post("/Admin/navigation/GetUserTopNavigations?searchStr=" + oldValue, {},
        function (data) {
          var menuUl = $(".sidebar-menu");
          menuUl.empty();
          menuUl.append(
            '<li class=""><a href= "/Home/UserPanel" ><i class="fa fa-home"></i> <span>首页</span></a ></li >');

          $(data).each(function () {
            var item = this;
            var parentLi = $('<li class="treeview">' +
              '<a href="#" data-code="' +
              item.code +
              '">' +
              '<i class="fa ' +
              item.image +
              '" ></i>' +
              '<span>' +
              item.name +
              '</span>' +
              '<span class="pull-right-container">' +
              '<i class="fa fa-angle-left pull-right"></i>' +
              '</span></a ></li>');

            menuUl.append(parentLi);

            if (item.items.length > 0) {
              var childUl = $("<ul class='treeview-menu'></ul>");

              $(item.items).each(function () {
                var childItem = this;
                var li = $('<li>' +
                  '<a href="#" data-code="' +
                  childItem.code +
                  '">' +
                  '<i class="fa ' +
                  childItem.image +
                  '" ></i>' +
                  ' <span>' +
                  childItem.name +
                  '</span>' +
                  '</a ></li>');

                childUl.append(li);
              });

              parentLi.append(childUl);
            }
          });
          layer.closeAll('loading');
        });
    }
  }

  $("#searchStr").on('keypress', function (event) {
    if (event.keyCode === 13) {
      needReloadMenu = true;
      onReloadMenuTree();
    }
  });

  $("#searchStr").on('focus', function (event) {
    oldValue = $("#searchStr").val();
  });

  $("#searchStr").on('blur', function (event) {
    needReloadMenu = $("#searchStr").val() !== oldValue;
    onReloadMenuTree();
  });

  onReloadMenuTree();
}

//处理邮件
var handleMail = function () {
  abp.ajax({ url: "/api/services/mail/message/getMessageMainOutputs" }).done(function(data) {
      $(".label-mail-count").text(data.length);
      var mailUl = $("#mailTableMenu");
    mailUl.empty();

      $(data).each(function () {
        var li =
          '<li data-id="'+this.Id+'"><a href="#">' +
            '<div class="pull-left">' +
            '<img src="/Admin/User/GetUserHeadImgById?userId=' + this.SenderId+'" class="img-circle" alt="User Image" />' +
            '</div>' +
            '<h4>' + this.SenderName + '<small><i class="fa fa-clock-o"></i> ' + this.CreateDateTime.ToDateTime().GetIntervalText()+'</small></h4>' +
            '<p>' + this.Title+'</p></a></li >';

        mailUl.append(li);
    });

      mailUl.find("li").click(function () {
          var sendId = $(this).data("id");
        var title = $(this).find("p").text();
        var url = "/mail/message/show?type=recive&sendId=" + sendId;

       addTabs(title, url);
    });
  });

  $("#btnCheckAllMail").click(function() {
    addTabs("内部邮件", "/mail/message/index", true);
  });
}

//处理通知消息
var handleNotify = function() {
    abp.ajax({ url: "/api/services/mail/notification/GetNotificationMainOutputs" }).done(function (data) {
        $(".label-notification-count").text(data.length);
    var mailUl = $("#notificationTableMenu");
    mailUl.empty();

    $(data).each(function () {
      var li =
          ' <li> <a href="#" ><i class="' + this.ImgFont+'"></i> ' + this.MessageContent+'</a ></li >';

      mailUl.append(li);
    });
  });
}

//处理退出登录
var handleLoginOut = function () {
    $("#btnLogOut").click(function () {
        layer.confirm('退出系统将会返回登录界面?', function (index) {
          window.location.href = '/account/Logout';
      });    
  });
}

//处理皮肤
var handleSkin = function() {
    $(".z-setTheme-color li").click(function () {
      var li = $(this);
      var name = li.data("name");
      var css = li.data("css");
      var currCss = $(".skin-this").data("css");

      if (!li.hasClass("skin-this")) {
          abp.ajax({ url: "/api/services/admin/KelonProudSetting/ChangeUiThemeSetting?value=" + name }).done(function() {
          $(".skin-this").removeClass("skin-this");
          li.addClass("skin-this");
          $("body").removeClass(currCss);
          $("body").addClass(css);
        });
      }
    });
}